import csv
import re

import requests
from lxml import etree
from pip._vendor.retrying import retry


class Guazi():
    def __init__(self):
        self.url = "https://www.guazi.com/suqian/buy/o%s"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
            "Cookie": "uuid=db2983f3-a348-46e6-df02-7674bd9b1e49; cityDomain=suqian; clueSourceCode=%2A%2300; user_city_id=292; ganji_uuid=4955962950848269093421; sessionid=d7baf871-e7c5-4a05-9840-f049386f8704; lg=1; Hm_lvt_bf3ee5b290ce731c7a4ce7a617256354=1605140663; close_finance_popup=2020-11-12; antipas=154517uT47454K5881P2z13961; preTime=%7B%22last%22%3A1605142573%2C%22this%22%3A1605140650%2C%22pre%22%3A1605140650%7D; cainfo=%7B%22ca_a%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_s%22%3A%22seo_baidu%22%2C%22ca_n%22%3A%22default%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22-%22%2C%22ca_campaign%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22display_finance_flag%22%3A%22-%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%22db2983f3-a348-46e6-df02-7674bd9b1e49%22%2C%22ca_city%22%3A%22nj%22%2C%22sessionid%22%3A%22d7baf871-e7c5-4a05-9840-f049386f8704%22%7D; Hm_lpvt_bf3ee5b290ce731c7a4ce7a617256354=1605142577"

        }
        self.is_one = True

    def start(self, num):
        for i in range(1, num + 1):
            url = self.url % i

            response = self.get_response(url)

            result = self.save_data(response)

            if result:
                print("第%s页数据下载成功" % i)
            else:
                print("数据错误")

    @retry(stop_max_attempt_number=3)
    def get_response(self, url):

        try:
            response = requests.get(url=url, headers=self.headers)

            assert response.status_code == 200
            return response

        except Exception as e:
            print(e)
            raise Exception("请求发送错误")

    def save_data(self, response):

        html = response.content.decode()

        html = etree.HTML(html)

        # 车名字
        car_names = html.xpath('//h2[@class="t"]/text()')

        # 车的年份 路程
        car_year_and_car_distance_list = re.findall(
            r'<div class="t-i">(.*?)<span class="icon-pad">\|</span>(.*?)<span class=\'icon-pad\'>|</span></div>',
            response.content.decode())

        # 车的原价
        car_price_and_danwei_yuanjia_list = re.findall(
            r'<div class="t-price">\s*<!-- 价格 -->\s*<!-- 如果是保卖车且有降价,则是\'补贴金\'的话术样式-->\s*<p>(.*?)<span>(.*?)</span>\s*</p>\s*<!-- 标签 -->\s*[\s\S]*?<em class=\'line-through\'>(.*?)</em>\s*</div>',
            response.content.decode())

        data_header = ["名字", "年份", "路程", "价格", "原价"]

        with open("瓜子二手车数据.csv", "a", encoding="utf-8") as f:
            f = csv.writer(f)
            if self.is_one:
                f.writerow(data_header)
                self.is_one = False
            for i in range(len(car_names)):
                f.writerow([car_names[i], car_year_and_car_distance_list[i][0], car_year_and_car_distance_list[i][1],
                            car_price_and_danwei_yuanjia_list[i][0] + car_price_and_danwei_yuanjia_list[i][1],
                            car_price_and_danwei_yuanjia_list[i][2]])

        return True


if __name__ == '__main__':
    g = Guazi()

    g.start(10)
